import type { MRT_ColumnDef } from 'material-react-table'
import { z } from 'zod'

import { baseManyObjectsScheme, baseSchemeWithNote } from '@/types/base.types'

import { phoneRegex } from '@/config/zod.config'

export const clientsColumns: MRT_ColumnDef<IClient>[] = [
	{
		header: 'ID',
		accessorKey: 'id',
		size: 100,
		enablePinning: true
	},
	{
		accessorFn: row => `${row.first_name} ${row.last_name}`,
		header: 'Имя Фамилия',
		id: 'full_name',
		enablePinning: true,
		enableClickToCopy: true
	},
	{
		header: 'Телефон',
		accessorKey: 'phone_number',
		enableClickToCopy: true
	},
	{
		header: 'Почта',
		accessorKey: 'email',
		enableClickToCopy: true
	},
	{
		header: 'Дата добавления',
		accessorFn: row => row.created_on.format('HH:mm DD.MM.YYYY')
	},
	{
		header: 'Последнее изменение',
		accessorFn: row => row.updated_on.format('HH:mm DD.MM.YYYY')
	}
]

export const clientScheme = baseSchemeWithNote.extend({
	first_name: z.string(),
	last_name: z.string(),
	phone_number: z.string(),
	email: z.string(),
	note: z.string(),
	requests: z.array(z.number()),
	orders: z.array(z.number()),
	contracts: z.array(z.number())
})

export type IClient = z.infer<typeof clientScheme>

export const clientFormScheme = z.object({
	first_name: z.string().min(2).max(35),
	last_name: z.string().min(2).max(35),
	email: z
		.string()
		.email('Некорректный адрес электронной почты')
		.or(z.literal('')),
	phone_number: z.string().regex(phoneRegex, 'Некорректный номер телефона'),
	note: z.string()
})

export type IClientForm = z.infer<typeof clientFormScheme>

export const clientsScheme = baseManyObjectsScheme.extend({
	results: z.array(clientScheme)
})

export type IClients = z.infer<typeof clientsScheme>
